/*
 * MobCatAdminView.java
 */
package mobcatadmin;

import dialogs.EscolhaProjeto;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import dialogs.MobCatTree;
import dialogs.PortalCfgDlg;
import java.awt.Cursor;
import javax.swing.JOptionPane;
import mobcatwebappgeneration.BuildMobile;
import persistencia.controller.jpa.JdbcController;
import persistencia.controller.jpa.PortalCfgJpaController;
import persistencia.entidade.PortalCfg;

/**
 * The application's main frame.
 */
public class MobCatAdminView extends FrameView {

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public MobCatAdminView(SingleFrameApplication app) {
        super(app);
        initComponents();
        jSplitPane1.setLeftComponent(new MobCatTree(this));

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });


    }

    public JSplitPane getJSplitPane1() {
        return jSplitPane1;
    }

    public JTabbedPane getTabCategoria() {
        return tabCategoria;
    }

    public JTabbedPane getTabRegistro() {
        return tabRegistro;
    }

    @Action
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = MobCatAdminApp.getApplication().getMainFrame();
            aboutBox = new MobCatAdminAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MobCatAdminApp.getApplication().show(aboutBox);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane2 = new javax.swing.JSplitPane();
        tabCategoria = new javax.swing.JTabbedPane();
        tabRegistro = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        menuAbrirProjeto = new javax.swing.JMenuItem();
        menuNovoProjeto = new javax.swing.JMenuItem();
        menuEditarProjeto = new javax.swing.JMenuItem();
        menuExcluirProjeto = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        menuMobile = new javax.swing.JMenu();
        menuBuild = new javax.swing.JMenuItem();
        menuAgendamento = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        jSplitPane1.setDividerLocation(300);
        jSplitPane1.setLastDividerLocation(300);
        jSplitPane1.setName("jSplitPane1"); // NOI18N
        jSplitPane1.setOneTouchExpandable(true);

        jSplitPane2.setDividerLocation(200);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane2.setName("jSplitPane2"); // NOI18N
        jSplitPane2.setOneTouchExpandable(true);

        tabCategoria.setName("tabCategoria"); // NOI18N
        jSplitPane2.setLeftComponent(tabCategoria);

        tabRegistro.setName("tabRegistro"); // NOI18N
        jSplitPane2.setRightComponent(tabRegistro);

        jSplitPane1.setRightComponent(jSplitPane2);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getResourceMap(MobCatAdminView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(mobcatadmin.MobCatAdminApp.class).getContext().getActionMap(MobCatAdminView.class, this);
        menuAbrirProjeto.setAction(actionMap.get("refreshMenuAction")); // NOI18N
        menuAbrirProjeto.setText(resourceMap.getString("menuAbrirProjeto.text")); // NOI18N
        menuAbrirProjeto.setActionCommand(resourceMap.getString("menuAbrirProjeto.actionCommand")); // NOI18N
        menuAbrirProjeto.setName("menuAbrirProjeto"); // NOI18N
        menuAbrirProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAbrirProjetoActionPerformed(evt);
            }
        });
        fileMenu.add(menuAbrirProjeto);

        menuNovoProjeto.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        menuNovoProjeto.setText(resourceMap.getString("menuNovoProjeto.text")); // NOI18N
        menuNovoProjeto.setName("menuNovoProjeto"); // NOI18N
        menuNovoProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuNovoProjetoActionPerformed(evt);
            }
        });
        fileMenu.add(menuNovoProjeto);

        menuEditarProjeto.setText(resourceMap.getString("menuEditarProjeto.text")); // NOI18N
        menuEditarProjeto.setName("menuEditarProjeto"); // NOI18N
        menuEditarProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditarProjetoActionPerformed(evt);
            }
        });
        fileMenu.add(menuEditarProjeto);

        menuExcluirProjeto.setText(resourceMap.getString("menuExcluirProjeto.text")); // NOI18N
        menuExcluirProjeto.setName("menuExcluirProjeto"); // NOI18N
        menuExcluirProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuExcluirProjetoActionPerformed(evt);
            }
        });
        fileMenu.add(menuExcluirProjeto);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        menuMobile.setText(resourceMap.getString("menuMobile.text")); // NOI18N
        menuMobile.setName("menuMobile"); // NOI18N
        menuMobile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuMobileActionPerformed(evt);
            }
        });

        menuBuild.setText(resourceMap.getString("menuBuild.text")); // NOI18N
        menuBuild.setName("menuBuild"); // NOI18N
        menuBuild.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuBuildActionPerformed(evt);
            }
        });
        menuMobile.add(menuBuild);

        menuAgendamento.setText(resourceMap.getString("menuAgendamento.text")); // NOI18N
        menuAgendamento.setName("menuAgendamento"); // NOI18N
        menuMobile.add(menuAgendamento);

        menuBar.add(menuMobile);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 503, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void menuNovoProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNovoProjetoActionPerformed

        PortalCfgDlg p = new PortalCfgDlg(this.getFrame(), new PortalCfg());
        p.setVisible(true);
    ((MobCatTree)jSplitPane1.getLeftComponent()).menuNovoProjetoActionPerformed(evt, this.getFrame());

}//GEN-LAST:event_menuNovoProjetoActionPerformed

    private void menuAbrirProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAbrirProjetoActionPerformed
        EscolhaProjeto ep = new EscolhaProjeto(this.getFrame(), ((MobCatTree) jSplitPane1.getLeftComponent()), true);
        ep.show(true);
    }//GEN-LAST:event_menuAbrirProjetoActionPerformed

    private void menuExcluirProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExcluirProjetoActionPerformed

        ((MobCatTree) jSplitPane1.getLeftComponent()).menuExcluirProjetoActionPerformed(evt, this.getFrame());

}//GEN-LAST:event_menuExcluirProjetoActionPerformed

    private void menuMobileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuMobileActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuMobileActionPerformed

    private void menuBuildActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuBuildActionPerformed

        if (JdbcController.getInstance().getSchema() == null || JdbcController.getInstance().getSchema().isEmpty()) {
           JOptionPane.showMessageDialog(this.getFrame(), "Selecione um projeto para realizar a operação", "Aviso", JOptionPane.PLAIN_MESSAGE);
        } else {
            Cursor c = this.getFrame().getCursor();
            this.getFrame().setCursor(new Cursor(Cursor.WAIT_CURSOR));
            BuildMobile.getInstance().execute(null);
            this.getFrame().setCursor(c);
        }

    }//GEN-LAST:event_menuBuildActionPerformed

    private void menuEditarProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditarProjetoActionPerformed

        if (JdbcController.getInstance().getSchema() == null || JdbcController.getInstance().getSchema().isEmpty()) {
            JOptionPane.showMessageDialog(this.getFrame(), "Selecione um projeto para realizar a operação", "Aviso", JOptionPane.PLAIN_MESSAGE);
        } else {

            PortalCfgDlg p = new PortalCfgDlg(this.getFrame(), new PortalCfgJpaController().getActivePortal());
            p.setVisible(true);
            
        }
    }//GEN-LAST:event_menuEditarProjetoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem menuAbrirProjeto;
    private javax.swing.JMenuItem menuAgendamento;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem menuBuild;
    private javax.swing.JMenuItem menuEditarProjeto;
    private javax.swing.JMenuItem menuExcluirProjeto;
    private javax.swing.JMenu menuMobile;
    private javax.swing.JMenuItem menuNovoProjeto;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTabbedPane tabCategoria;
    private javax.swing.JTabbedPane tabRegistro;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;

    /**
     * @return the jSplitPane2
     */
    public javax.swing.JSplitPane getJSplitPane2() {
        return jSplitPane2;
    }
}
